// ignore_for_file: file_names, slash_for_doc_comments, must_call_super, unnecessary_overrides, override_on_non_overriding_member, must_be_immutable, prefer_const_constructors, sized_box_for_whitespace, avoid_unnecessary_containers, avoid_print

import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:mybase/base/abstract/IFFR.dart';
import 'package:mybase/base/abstract/IFFRefresh.dart';
import 'package:mybase/base/abstract/IFFScaffold.dart';
import 'package:mybase/base/tool/empty/FFEmpty.dart';
import 'package:mybase/base/tool/entitylog/MyPageTestRun.dart';
import 'package:mybase/base/tool/file/FFDirectory.dart';
import 'package:mybase/base/tool/file/FFile.dart';
import 'package:mybase/base/widget/layout/FFLineContainer.dart';
import 'package:mybase/base/widget/layout/FFTextButton.dart';
import 'package:mybase/base/widget/page/FFScaffold.dart';
import 'package:mybase/base/tool/extension/FFListExt.dart';
import 'package:webview_window_example/config/ConfigVal.dart';
import 'package:webview_window_example/textField/FFTextField.dart';
import 'package:webview_window_example/utils/navi/AppNavi.dart';

class CreateTemplateCodePage extends FFScaffold {
  String? name;

  CreateTemplateCodePage({
    Key? key,
    this.name,
  }) : super(key: key);

  @override
  CreateTemplateCodePageState createState() => CreateTemplateCodePageState();
}

class CreateTemplateCodePageState extends FFScaffoldState with MFFScaffold {
  /**固定写法 */
  @override
  IFFScaffold get iffScaffold => this;

  @override
  CreateTemplateCodePage get widget => super.widget as CreateTemplateCodePage;

  @override
  IFFRefresh? get iffRefresh => refresh;
  late CreateTemplateCodePageRefresh refresh =
      CreateTemplateCodePageRefresh(state: this);

  /**固定写法end */

  String initTemplateFilePath = ConfigVal.templateCodeDir;
  String initTargetDirPath = ConfigVal.osDir;
  String? templateFile;
  String? templateClassName;
  String? targetDir;
  String? className;

  @override
  void ffInitState() {
    super.ffInitState();
  }

  @override
  Widget? ffNavi(BuildContext context) {
    return AppNavi.createNavi(
      context,
      title: "创建模板",
    );
  }

  @override
  Widget? ffBody(BuildContext context) {
    return FFLineContainer(
      direction: Axis.vertical,
      padding: EdgeInsets.symmetric(horizontal: 10),
      children: [
        FFTextButton(
          unconstrainedBox: true,
          text: "选择模板路径",
          padding: EdgeInsets.symmetric(horizontal: 12, vertical: 8),
          color: Colors.red,
          margin: EdgeInsets.only(top: 8),
          onClick: () async {
            FilePickerResult? result = await FilePicker.platform.pickFiles(
              type: FileType.custom,
              allowedExtensions: ['txt', 'dart'],
              initialDirectory: initTemplateFilePath,
            );

            if (!(await FFile.isExistFile(result?.files.single.path ?? ""))) {
              SmartDialog.showToast("目标文件选择失败");
              return;
            }

            templateFile = result!.files.single.path;
            templateClassName = templateFile?.split("/").last.split(".").first;
            setState(() {});
          },
        ),
        FFTextButton(
          text: templateFile,
          maxLines: 1000,
        ),
        FFTextButton(
          unconstrainedBox: true,
          text: "选择生成的目录",
          padding: EdgeInsets.symmetric(horizontal: 12, vertical: 8),
          color: Colors.red,
          margin: EdgeInsets.only(top: 8),
          onClick: () async {
            String? selectedDirectory =
                await FilePicker.platform.getDirectoryPath(initialDirectory: initTargetDirPath);

            if (!(await FFDirectory.isExistDirectory(
                selectedDirectory ?? ""))) {
              SmartDialog.showToast("目标目录选择失败");
              return;
            }

            targetDir = selectedDirectory;
            setState(() {});
          },
        ),
        FFTextButton(
          text: targetDir,
          maxLines: 1000,
        ),
        FFTextButton(
          unconstrainedBox: true,
          text: "自定义类名",
          padding: EdgeInsets.symmetric(horizontal: 12, vertical: 8),
          color: Colors.red,
          margin: EdgeInsets.only(top: 8),
        ),
        Container(
          width: 200,
          height: 36,
          color: Colors.pink,
          padding: EdgeInsets.symmetric(horizontal: 10),
          child: FFTextField(
            placeStr: "请输入类名",
            needCancelBtn: false,
            onValid: (bool valid, String value) {
              className = value;
            },
            min: 1,
            max: 99,
          ),
        ),
        FFTextButton(
          unconstrainedBox: true,
          text: "创建",
          padding: EdgeInsets.symmetric(horizontal: 12, vertical: 8),
          color: Colors.red,
          margin: EdgeInsets.only(top: 8),
          onClick: () async {
            if (!(await FFile.isExistFile(templateFile ?? ""))) {
              SmartDialog.showToast("模板文件不存在");
              return;
            }

            if (FFEmpty.stringIsEmpty(className)) {
              SmartDialog.showToast("自定义类名不能为空");
              return;
            }

            String result = await MyPageTestRun.createPageCode2(
              templateFile: templateFile!,
              templateClassName: templateClassName!,
              targetDir: targetDir!,
              className: className!,
            );
            SmartDialog.showToast(result);

          },
        ),
      ],
    );
  }
}

class CreateTemplateCodePageRefresh with MFFRefresh {
  CreateTemplateCodePageState state;

  CreateTemplateCodePageRefresh({required this.state});

  @override
  bool? ffCanRefreshHead() {
    return true;
  }

  @override
  ffRefresh(bool isHead) {
    ffRefreshResult(isHead, test, state);
  }

  //具体请求业务
  Future<IFFR> test(IRefreshParam? iRefreshParam) async {
    List? list;
    await Future.wait([
      //HomeApi.index_recentlyPlayed(),
    ]).then((value) {
      list = value;
    });

    IFFR? r = list?.safeValue(0);
    return r ?? R(rStatus: RStatus.result);
  }
}
